package srst.util;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



/**
 * @author YuanYuhu
 */
public class FileUtil {
	
	
	/**
	 * 获取指定文件夹下的所有文件
	 * @param path
	 * @return
	 */
	public  static List<File> getFileList(String SinglePath){  
		List<File> files = new ArrayList<File>();
		File file = new File(SinglePath);    
		File[] array = file.listFiles();
		for(int i=0;i<array.length;i++){    
			files.add(array[i]);
		} 
		return files;
	}
	public  static Map<String, File> getFileMap(String SinglePath){
		Map<String, File> fileMap = new HashMap<>();
		File dir = new File(SinglePath);   
		File[] array = dir.listFiles();
		
		for(File file : array ){    
			String fileName = file.getName();
			fileName = fileName.substring(0,fileName.indexOf("."));
			fileMap.put(fileName, file);
		} 
		return fileMap;
	}
	
	
	public static FileOutputStream writeFile(String filename) {
		FileOutputStream out = null;
		try {
			out = new FileOutputStream(filename);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return out;
	}
	
	
	public static BufferedWriter getWriter(String outpath){
		FileOutputStream fos = null;
		BufferedWriter writer = null;
		try{
			fos = new FileOutputStream(new File(outpath),true);      
			writer = new BufferedWriter(new OutputStreamWriter(fos,"utf-8") );
		}catch(IOException e){
			e.printStackTrace();
			try {
				writer.close();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
		return writer;
		
	}
	
	private static String getCharset(File fileName) throws IOException{  
        
        BufferedInputStream bin = new BufferedInputStream(new FileInputStream(fileName));    
        int p = (bin.read() << 8) + bin.read();    
          
        String code = null;    
          
        switch (p) {    
            case 0xefbb:    
                code = "UTF-8";    
                break;    
            case 0xfffe:    
                code = "Unicode";    
                break;    
            case 0xfeff:    
                code = "UTF-16BE";    
                break;    
            default:    
                code = "GBK";    
        }    
        return code;  
	}  
	
	public static BufferedReader getReader(String inpath){
		InputStreamReader read = null;
		BufferedReader reader = null;
		try{
			read = new InputStreamReader(new FileInputStream(new File(inpath)),"utf-8");
			reader = new BufferedReader(read);
		}catch(IOException e){
			try {
				reader.close();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			e.printStackTrace();
		}
		return reader;
	}
	
	public static BufferedReader getReader(File file){
		InputStreamReader read = null;
		BufferedReader reader = null;
		try{
			read = new InputStreamReader(new FileInputStream(file),"utf-8");
			reader = new BufferedReader(read);
		}catch(IOException e){
			try {
				reader.close();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			e.printStackTrace();
		}
		return reader;
	}
	
	public static void write2File(List<String> texts, String filePath){
		BufferedWriter writer = FileUtil.getWriter(filePath);
		try {
			for(String text : texts){
				writer.write(text);
				writer.newLine();
				writer.flush();
			}
		
			if(writer != null){
				writer.close();
			}
		
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static String readRegex(String filePath) {
		String regex="";
		File file=new File(filePath);
		try {
			InputStreamReader read = new InputStreamReader(new FileInputStream(file),"UTF-8");//�㻹��̫�����ˣ��Ǻ�
			BufferedReader bufferedReader = new BufferedReader(read);
			int k=	getTotalLines(file);
			String lineTxt = null;
			int i=0;
			while((lineTxt = bufferedReader.readLine()) != null){
				if(i!=k-1){
					regex=regex+"("+lineTxt+")"+"|";
				}else{
					regex=regex+lineTxt;
				}
				i++;
			}
			read.close();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return regex;
	}
	
	public static int getTotalLines(File file) throws IOException {  
		FileReader in = new FileReader(file);  
		LineNumberReader reader = new LineNumberReader(in);  
		String s = reader.readLine();  
		int lines = 0;  
		while (s != null) {  
			lines++;  
			s = reader.readLine();  
		}  
		reader.close();  
		in.close();  
		return lines;  
	}  
}





